<?php

require_once APPPATH . 'dao/configurationManager.php';

class Layers extends CI_Controller {

    public function index() {
        if ($this->session->userdata('logged_in')) {
            if (!$this->input->post('submit')) {
                $this->initView();
            } else {
                $data = array();
                $data['layer'] = $this->input->post('layer');
                $data['srs'] = $this->input->post('srs');
                $data['format'] = $this->input->post('format');
                
                add_css(array('/assets/css/viewLayer.css'));
                add_js(array('assets/libs/Openlayers/OpenLayers.js',
                    'assets/libs/Openlayers/proj4js-compressed.js',
                    'assets/libs/Openlayers/defs/EPSG22185.js',
                    'assets/js/util.js'));
                $this->load->view('pages/header.php');
                $this->load->view('pages/viewLayer.php', $data);
                $this->load->view('pages/footer.php');
            }
        } else {
            redirect('/login');
        }
    }

    private function initView() {
        $data = array();

        $layer = array('name' => 'layer',
            'id' => 'layer',
            //'value' => '', // set_value('bboxMinX'),
            'maxlength' => 100,
            'size' => 100,
            'type' => 'hidden'
        );
        $srs = array('name' => 'srs',
            'id' => 'srs',
            //'value' => '', // set_value('bboxMinX'),
            'maxlength' => 15,
            'size' => 15,
            'type' => 'hidden'
        );
        $format = array('name' => 'format',
            'id' => 'format',
            //'value' => '', // set_value('bboxMinX'),
            'maxlength' => 20,
            'size' => 20,
            'type' => 'hidden'
        );
        $bbox = array('name' => 'bbox',
            'id' => 'bbox',
            //'value' => '', // set_value('bboxMinX'),
            'maxlength' => 20,
            'size' => 20,
            'type' => 'hidden'
        );
        $minZoomLevel = array('name' => 'minZoomLevel',
            'id' => 'minZoomLevel',
            //'value' => '', // set_value('bboxMinX'),
            'maxlength' => 3,
            'size' => 5,
            'type' => 'hidden'
        );
        $maxZoomLevel = array('name' => 'maxZoomLevel',
            'id' => 'maxZoomLevel',
            //'value' => '', // set_value('bboxMinX'),
            'maxlength' => 3,
            'size' => 5,
            'type' => 'hidden'
        );


        $data['layer'] = $layer;
        $data['srs'] = $srs;
        $data['format'] = $format;
        $data['bbox'] = $bbox;
        $data['minZoomLevel'] = $minZoomLevel;
        $data['maxZoomLevel'] = $maxZoomLevel;

        $attr = array('style' => 'display:none;', 'id' => 'viewLayerForm', 'target' => '_blank');

        $data['attr'] = $attr;
        add_js(array('assets/libs/jqGrid/js/i18n/grid.locale-es.js',
            'assets/libs/jqGrid/js/jquery.jqGrid.min.js',
            'assets/libs/Openlayers/OpenLayers.js',
            'assets/js/wmtsLayer.js'));
        add_css(array('/assets/libs/jqGrid/css/ui.jqgrid.css', '/assets/css/layers.css'));
        
        $this->load->view('pages/header.php');
        $this->load->view('pages/layers.php', $data);
        $this->load->view('pages/footer.php');
    }

    public function getInitialDataForGrid() {
        $data = array();
        $data['grids'] = ConfigurationManager::getInstance()->getSupportedGridsData();
        $data['layers'] = ConfigurationManager::getInstance()->getAllTilesetsData();
        echo json_encode($data);
    }
   

}